package com.spaceraccoons.api.adapter;

import org.joda.time.DateMidnight;
import org.joda.time.DateTimeZone;

import com.spaceraccoons.api.types.DateTimeProtos;


/**
 * @author Benjamin P. Jung
 */
public class DateAdapter extends ProtobufAdapter<DateTimeProtos.Date, DateMidnight> {

	@Override
	public DateMidnight unmarshal(final DateTimeProtos.Date pbDate) {

		final DateMidnight dateMidnight = new DateMidnight(
				pbDate.getYear(),
				pbDate.getMonthOfYear(),
				pbDate.getDayOfMonth(),
				DateTimeZone.forID(pbDate.getTimeZone()));
		return dateMidnight;
		
	}

	@Override
	public DateTimeProtos.Date marshal(final DateMidnight date) {

		return DateTimeProtos.Date.newBuilder()
				.setYear(date.getYear())
				.setMonthOfYear(date.getMonthOfYear())
				.setDayOfMonth(date.getDayOfMonth())
				.setTimeZone(date.getChronology().getZone().getID())
				.build();

	}

}
